package cn.sytton.taffe.log.service.impl;

import cn.sytton.taffe.log.entity.UserLogEntity;
import cn.sytton.taffe.log.mapper.UserLogMapper;
import cn.sytton.taffe.log.service.IUserLogService;
import cn.sytton.taffe.framework.base.service.BaseService;
import cn.sytton.taffe.framework.util.SecurityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.util.Date;

/**
 * 用户操作日志服务层实现
 *
 * @author skyrock
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserLogService extends BaseService<UserLogMapper, UserLogEntity> implements IUserLogService {

    @Override
    public void create(UserLogEntity entity) {
        entity.setUserId(SecurityUtil.getUserId());
        entity.setUserCode(SecurityUtil.getUserCode());
        entity.setUserName(SecurityUtil.getUserName());
        entity.setLoginName(SecurityUtil.getLoginName());
        entity.setAction(StringUtils.hasText(entity.getAction()) ? entity.getAction() : "打开菜单");
        entity.setLogTime(new Timestamp(new Date().getTime()));
        entity.setIp(SecurityUtil.getIp());
        entity.setLocation(SecurityUtil.getLoginLocation());
        entity.setBrowser(SecurityUtil.getBrowser());
        entity.setOs(SecurityUtil.getOs());
        super.create(entity);
    }
}
